<?php
App::uses('AppController', 'Controller');
/**
 * Gastos Controller
 *
 * @property Gasto $Gasto
 * @property PaginatorComponent $Paginator
 */
class GastosController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Gasto->recursive = 0;
		$this->set('gastos', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Gasto->exists($id)) {
			throw new NotFoundException(__('Invalid gasto'));
		}
		$options = array('conditions' => array('Gasto.' . $this->Gasto->primaryKey => $id));
		$this->set('gasto', $this->Gasto->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Gasto->create();
			if ($this->Gasto->saveAssociated($this->request->data,array('deep' => true))) {
				$this->Session->setFlash(__('Datos grabados correctamente.'));
				//return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se grabaron los datos.'));
			}
		}
		$metas = $this->Gasto->Meta->find('list');
		$fuentes = $this->Gasto->Fuente->find('list');
		$planContables = $this->Gasto->PlanContable->find('all',array(
			'fields' => array('id','codigo','name'),
			'order' => array('codigo')
			));
		$clasificadors = $this->Gasto->Clasificador->find('all',array(
			'fields' => array('id','codigo','name'),
			'order' => array('codigo')
			));
		$documentoTipos = $this->Gasto->DocumentoTipo->find('list');
		$this->set(compact('metas', 'fuentes', 'documentoTipos'));
		$this->set(array(
			'clasificadors' => $clasificadors,
			'planContables' => $planContables));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Gasto->exists($id)) {
			throw new NotFoundException(__('Invalid gasto'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Gasto->save($this->request->data)) {
				$this->Session->setFlash(__('The gasto has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gasto could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Gasto.' . $this->Gasto->primaryKey => $id));
			$this->request->data = $this->Gasto->find('first', $options);
		}
		$metas = $this->Gasto->Meta->find('list');
		$fuentes = $this->Gasto->Fuente->find('list');
		$planContables = $this->Gasto->PlanContable->find('list');
		$clasificadors = $this->Gasto->Clasificador->find('list');
		$documentoTipos = $this->Gasto->DocumentoTipo->find('list');
		$this->set(compact('metas', 'fuentes', 'planContables', 'clasificadors', 'documentoTipos'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Gasto->id = $id;
		if (!$this->Gasto->exists()) {
			throw new NotFoundException(__('Invalid gasto'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Gasto->delete()) {
			$this->Session->setFlash(__('The gasto has been deleted.'));
		} else {
			$this->Session->setFlash(__('The gasto could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
